package com.loongxun.playwithyou.ui.activity.playuser;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.TextView;

import com.loongxun.playwithyou.ApiInterface;
import com.loongxun.playwithyou.App;
import com.loongxun.playwithyou.R;
import com.loongxun.playwithyou.bean.Order;
import com.loongxun.playwithyou.bean.SendRequest;
import com.loongxun.playwithyou.network.MyHttpClientRespenseHandler;
import com.loongxun.playwithyou.network.nHttpClient;
import com.loongxun.playwithyou.util.ToastView;
import com.nostra13.universalimageloader.core.ImageLoader;

import org.json.JSONObject;

/**
 * 陪玩人员订单详情
 * Created by Administrator on 2017/7/21.
 */
public class OrderInfoActivity extends Activity {

    private Context context;
    private SendRequest send;
    private String orderNo;
    private Order order = new Order();
    private ImageLoader imageLoader = ImageLoader.getInstance();

    //标题
    private TextView tv_top_title;
    private ImageView iv_nm_img;
    private TextView tv_nm_name,
            tv_nm_item_order_no,tv_nm_item_start_time,tv_nm_item_hours_num,
            tv_nm_item_type, tv_nm_item_prive,tv_nm_item_address,
            tv_nm_item_address_hint,tv_nm_item_phone,tv_nm_item_state;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_order_info);
        context = OrderInfoActivity.this;

        orderNo = getIntent().getStringExtra("orderNo");

        try {
            send = new SendRequest();
            send.orderNo = orderNo;
            //判断此会员是否为陪玩人员
            nHttpClient.getAll(ApiInterface.ORDER_INFO, send.toOrderMemberInfoJson().toString(), orderMemberHandler);
        }catch (Exception e){
            ToastView toast = new ToastView(context, "参数错误");
            toast.show();
        }
        initView();
    }

    private void initView() {
        tv_top_title = (TextView) findViewById(R.id.tv_top_title);
        tv_top_title.setText("订单详情");
        findViewById(R.id.iv_back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        findViewById(R.id.ll_user_info).setVisibility(View.VISIBLE);
        findViewById(R.id.ll_nm_item_order).setVisibility(View.VISIBLE);
        findViewById(R.id.ll_nm_address).setVisibility(View.GONE);
        findViewById(R.id.ll_nm_num).setVisibility(View.GONE);

        iv_nm_img = (ImageView) findViewById(R.id.iv_nm_img);
        tv_nm_name = (TextView) findViewById(R.id.tv_nm_name);

        findViewById(R.id.ll_nm_item_order).setVisibility(View.VISIBLE);
        tv_nm_item_order_no = (TextView) findViewById(R.id.tv_nm_item_order_no);
        tv_nm_item_start_time = (TextView) findViewById(R.id.tv_nm_item_start_time);
        tv_nm_item_hours_num = (TextView) findViewById(R.id.tv_nm_item_hours_num);

        tv_nm_item_type = (TextView) findViewById(R.id.tv_nm_item_type);
        tv_nm_item_prive = (TextView) findViewById(R.id.tv_nm_item_prive);
        tv_nm_item_address = (TextView) findViewById(R.id.tv_nm_item_address);

        tv_nm_item_address_hint = (TextView) findViewById(R.id.tv_nm_item_address_hint);
        tv_nm_item_phone = (TextView) findViewById(R.id.tv_nm_item_phone);
        tv_nm_item_state = (TextView) findViewById(R.id.tv_nm_item_state);
    }

    private void initData() {
        tv_nm_name.setText(order.getUser_name());
        if (!order.getUser_photo().equals("")) {
            if (order.getUser_photo().startsWith("http://wx.qlogo.cn/mmopen")) {
                imageLoader.displayImage(order.getUser_photo(), iv_nm_img, App.options);
            }else {
                imageLoader.displayImage(ApiInterface.IMAGES + order.getUser_photo(), iv_nm_img, App.options);
            }
        }

        tv_nm_item_order_no.setText("订  单  号："+order.getOrder_no());
        tv_nm_item_start_time.setText("预约时间："+order.getPlay_start_time());
        tv_nm_item_hours_num.setText("预约时长：共计" +order.getHours_num()+"小时");

        if (order.getGame_type() == 0) {
            tv_nm_item_type.setText("游戏类型：王者荣耀");
        }else {
            tv_nm_item_type.setText("游戏类型：英雄联盟");
        }
        tv_nm_item_prive.setText("订单金额："+order.getOrder_all_price()+"元");
        tv_nm_item_address.setText("陪练地址："+order.getAddressIntro());

        tv_nm_item_address_hint.setText(order.getAddressDetail());
        tv_nm_item_phone.setText("客户电话："+order.getUser_phone());
        if (order.getOrder_state() == 3) {
            tv_nm_item_state.setText("订单已完成");
        } else {
            tv_nm_item_state.setText("订单进行中");
        }
    }

    private MyHttpClientRespenseHandler orderMemberHandler = new MyHttpClientRespenseHandler() {

        @Override
        public void success(JSONObject json) {
            Integer succeed = json.optJSONObject("status").optInt("succeed", 0);
            if (succeed == 1) {
                try {
                    JSONObject orderObject = json.getJSONObject("data").getJSONObject("playOrder");

                    order.setOrder_no(orderObject.getString("order_no"));
                    order.setPlay_start_time(orderObject.getString("play_start_time"));
                    order.setHours_num(orderObject.getInt("hours_num"));
                    order.setOrder_all_price(orderObject.getDouble("order_all_price"));
                    order.setUser_phone(orderObject.getString("user_phone"));
                    order.setOrder_state(orderObject.getInt("order_state"));

                    String address = json.getJSONObject("data").getString("merchant");
                    if (!address.equals("null")) {
                        JSONObject addressObject = json.getJSONObject("data").getJSONObject("merchant");
                        order.setAddressIntro(addressObject.getString("addressIntro"));
                        order.setAddressDetail(addressObject.getString("addressDetail"));
                    }else {
                        order.setAddressIntro("");
                        order.setAddressDetail("");
                    }

                    String user = json.getJSONObject("data").getString("playUser");
                    if (!user.equals("null")) {
                        JSONObject userObject = json.getJSONObject("data").getJSONObject("playUser");
                        order.setUser_name(userObject.getString("user_name"));
                        order.setUser_photo(userObject.getString("user_photo"));
                    }else {
                        order.setUser_name("暂无昵称");
                        order.setUser_photo("");
                    }
                    initData();
                }catch (Exception e){
                    e.printStackTrace();
                }
            }else {
                String error_desc = json.optJSONObject("status").optString("error_desc");
                new ToastView(context, error_desc).show();
            }
        }

        @Override
        public void faile(String json) {
            new ToastView(context, json).show();
        }
    };


}
